package com.kh.pajx.sop.util.security.encoder;

import java.security.Key;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/**
 * DES加密解密工具类
 * @author:	azzcsimp
 * @Createdate:	2014年12月15日 下午4:58:22
 */
public class DESEncoderUtil {

	private static Key key;
	private static String KEY_STR = "Hnzm!@#$";// 密钥
	private static String CHARSETNAME = "UTF-8";// 编码
	private static String ALGORITHM = "DES";// 加密类型

	static {
		try {
			KeyGenerator generator = KeyGenerator.getInstance(ALGORITHM);
			generator.init(new SecureRandom(KEY_STR.getBytes()));
			key = generator.generateKey();
			generator = null;
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	/**
	 * 对str进行DES加密
	 * @param str
	 * @return
	 */
	public static String encrypt(String str) {
		BASE64Encoder base64encoder = new BASE64Encoder();
		try {
			byte[] bytes = str.getBytes(CHARSETNAME);
			Cipher cipher = Cipher.getInstance(ALGORITHM);
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] doFinal = cipher.doFinal(bytes);
			return base64encoder.encode(doFinal);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
	
	 /**
     * 对str进行DES解密
     * @param str
     * @return
     */
    public static String decrypt(String str) {
        BASE64Decoder base64decoder = new BASE64Decoder();
        try {
            byte[] bytes = base64decoder.decodeBuffer(str);
            Cipher cipher = Cipher.getInstance(ALGORITHM);
            cipher.init(Cipher.DECRYPT_MODE, key);
            byte[] doFinal = cipher.doFinal(bytes);
            return new String(doFinal, CHARSETNAME);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    
    public static void main(String[] args) {
		System.out.println(encrypt("jdbc:oracle:thin:@192.168.5.250:1521:orcl"));
		System.out.println(encrypt("oracle.jdbc.OracleDriver"));
		System.out.println(encrypt("home_school"));
		System.out.println(encrypt("hs123456"));
		System.out.println("========================================");
		System.out.println(encrypt("jdbc:oracle:thin:@10.96.246.43:1567:orcl"));
		System.out.println(encrypt("oracle.jdbc.OracleDriver"));
		System.out.println(encrypt("home_school"));
		System.out.println(encrypt("Hs_87539800"));
		//System.out.println(decrypt("7f0GhBq8UPSndRUJpi3NMRkWaAZBXNph"));
		System.out.println("========================================");
		System.out.println(encrypt("jdbc:oracle:thin:@192.168.5.250:1521:orcl"));
		System.out.println(encrypt("oracle.jdbc.OracleDriver"));
		System.out.println(encrypt("pajx_dbak"));
		System.out.println(encrypt("dbak123"));
		System.out.println("========================================");
		System.out.println("========================================");
		System.out.println("========================================");
		System.out.println(encrypt("jdbc:oracle:thin:@10.96.246.43:1567:orcl"));
		System.out.println(encrypt("oracle.jdbc.OracleDriver"));
		System.out.println(encrypt("home_school"));
		System.out.println(encrypt("Hs_87539800"));
		System.out.println("========================================");
		System.out.println(encrypt("jdbc:oracle:thin:@10.96.246.43:1567:orcl"));
		System.out.println(encrypt("oracle.jdbc.OracleDriver"));
		System.out.println(encrypt("dbak"));
		System.out.println(encrypt("dbak_87539800"));
	}
}
